﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;
using RelationTextBox;
using System.Reflection;
using System.Threading;

namespace AMix
{
    public partial class AmixSetupFrm : FrmCard
    {
        public AmixSetupFrm(ModuleClass mInfo)
            : base(mInfo)
        {
            InitializeComponent();
            eSRPDataSet.AmixSetup.CodUnitateColumn.DefaultValue = ModuleInfo.CodUnitate;
            eSRPDataSet.AmixSetup.PrimaryKeyColumn.DefaultValue = 0;
            eSRPDataSet.AmixSetup.LunaCurentaColumn.DefaultValue = 1;
            eSRPDataSet.AmixSetup.AnulCurentColumn.DefaultValue = 2000;
            eSRPDataSet.AmixSetup.AfiseazaAmortizareColumn.DefaultValue = true;
            eSRPDataSet.AmixSetup.IstoricAmortizareColumn.DefaultValue = false;
        }

       
        private void AmixSetupFrm_Load(object sender, EventArgs e)
        {
            if (!EsteArhiva())
            {
                AddButton("Salveaza", global::ERPStudio.Properties.Resources.salvare1, Salvare, ToolStripItemAlignment.Right, true);
                AddButton("Inchidere luna", global::ERPStudio.Properties.Resources._001_42, Inchidere_Luna, ToolStripItemAlignment.Right, false);
                AddButton("Deschide luna", global::ERPStudio.Properties.Resources._001_41, Deschidere_Luna, ToolStripItemAlignment.Right, false);
                AddButton("Calcul Amortizare", global::ERPStudio.Properties.Resources._001_08, Recalcul, ToolStripItemAlignment.Right, false);
                HandleSalvare += new EventHandler(Salvare);
            }
            this.listBndSourceMain.Add(amixSetupBindingSource);
            listTableAdaptor.Add(amixSetupTableAdapter.Adapter);

            SetCurentUnit(ModuleInfo.CodUnitate);


        }

        private void Inchidere_Luna(object sender, EventArgs e)
        {
            if (MessageBox.Show("Sunteti sigur ca doriti inchiderea lunii curente?",
                            "Intrebare",
                            MessageBoxButtons.YesNo,
                            MessageBoxIcon.Question) == DialogResult.No)
                return;
            SqlCommand cmd = null;
            MessageBox.Show("Atentie! Se vor inchide toate machetele. Salvati datele, acolo unde este cazul!",
                "Info",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);

            foreach (StudioForm fChild in MdiParent.MdiChildren)
            {
                if (fChild != this)
                    fChild.Close();
            }
            try
            {
                Cursor.Current = Cursors.AppStarting;
                cmd = new SqlCommand("[amix_InchideLuna2]", DeschideConexiune(workConnString));
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Luna", LunaNUD.Value.ToString()));
                cmd.Parameters.Add(new SqlParameter("@An", AnulNUD.Value.ToString()));
                cmd.Parameters.Add(new SqlParameter("@CodUnitate", ModuleInfo.CodUnitate.ToString()));
                cmd.Parameters.Add(new SqlParameter("@IDDocument", "0"));
                cmd.Parameters.Add(new SqlParameter("@InchideLuna", "1"));
                cmd.ExecuteNonQuery();

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(),
                "Eroare",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);
                return;
            }
            finally
            {

                InchideConexiune(cmd.Connection);
                Cursor.Current = Cursors.Default;
            }
            MessageBox.Show("Documentele curente au fost arhivate cu succes!",
                            "Info",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
            SetCurentUnit(ModuleInfo.CodUnitate);
        }

        private void Deschidere_Luna(object sender, EventArgs e)
        {

            DateTime prez = new DateTime(Convert.ToInt32(AnulNUD.Value), Convert.ToInt32(LunaNUD.Value), 1);
            DateTime ant = prez.AddMonths(-1);
            if (MessageBox.Show("Sunteti sigur ca doriti deschiderea lunii " + ant.Month.ToString() +"." + ant.Year.ToString() ,
                            "Intrebare",
                            MessageBoxButtons.YesNo,
                            MessageBoxIcon.Question) == DialogResult.No)
                return;
            SqlConnection con =DeschideConexiune(workConnString);
            SqlCommand cmdVer = new SqlCommand("SELECT COUNT(*) FROM [AmixRegistruBK] WHERE Luna=" + ant.Month.ToString() + " AND  An =" + ant.Year.ToString(), con);
            Object ret = cmdVer.ExecuteScalar();
            if (Convert.ToInt32(ret) == 0)
            {
                InchideConexiune(con);
                MessageBox.Show("Luna " + ant.Month.ToString() + "." + ant.Year.ToString() + " nu poate fi redeschisa deoarece nu exista nici o informatie arhivata!",
                                "Info",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                return;
            }


            SqlCommand cmd = null;
            MessageBox.Show("Atentie! Se vor inchide toate machetele. Salvati datele, acolo unde este cazul!",
                "Info",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);

            foreach (StudioForm fChild in MdiParent.MdiChildren)
            {
                if (fChild != this)
                    fChild.Close();
            }
            try
            {
                Cursor.Current = Cursors.AppStarting;
                cmd = new SqlCommand("[amix_DeschideLuna]", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Luna", ant.Month.ToString()));
                cmd.Parameters.Add(new SqlParameter("@An", ant.Year.ToString()));
                cmd.Parameters.Add(new SqlParameter("@CodUnitate", ModuleInfo.CodUnitate.ToString()));
                cmd.ExecuteNonQuery();

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(),
                "Eroare",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);
                return;
            }
            finally
            {
                InchideConexiune(con);
                Cursor.Current = Cursors.Default;
            }
            MessageBox.Show("Luna a fost redeschisa cu succes!",
                            "Info",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
            SetCurentUnit(ModuleInfo.CodUnitate);
        }

        private void Recalcul(object sender, EventArgs e)
        {
            SqlCommand cmd = null;
            MessageBox.Show("Atentie! Se vor inchide toate machetele. Salvati datele, acolo unde este cazul!",
                "Info",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);

            foreach (StudioForm fChild in MdiParent.MdiChildren)
            {
                if (fChild != this)
                    fChild.Close();
            }
            try
            {
                Cursor.Current = Cursors.AppStarting;
                cmd = new SqlCommand("[amix_InchideLuna2]", DeschideConexiune(workConnString));
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Luna", LunaNUD.Value.ToString()));
                cmd.Parameters.Add(new SqlParameter("@An", AnulNUD.Value.ToString()));
                cmd.Parameters.Add(new SqlParameter("@CodUnitate", ModuleInfo.CodUnitate.ToString()));
                cmd.Parameters.Add(new SqlParameter("@IDDocument", "0"));
                cmd.Parameters.Add(new SqlParameter("@InchideLuna", "0"));
                cmd.ExecuteNonQuery();

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(),
                "Eroare",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);
                return;
            }
            finally
            {

                InchideConexiune(cmd.Connection);
                Cursor.Current = Cursors.Default;
            }
            MessageBox.Show("Recalculul amortizarilor a fost finalizat cu succes!",
                            "Info",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
            SetCurentUnit(ModuleInfo.CodUnitate);
        }

        override public void SetCurentMonth(short vCodUnitate)
        {

        }

        override public void SetCurentUnit(short vCodUnitate)
        {
            this.amixSetupTableAdapter.Connection = DeschideConexiune(workConnString);
            this.amixSetupTableAdapter.Fill(this.eSRPDataSet.AmixSetup, vCodUnitate);
            if (amixSetupBindingSource.Count == 0)
            {
                amixSetupBindingSource.AllowNew = true;
                Adauga(this, null);
                amixSetupBindingSource.AllowNew = false;
                AnulNUD.Enabled = true;
                AnulNUD.ReadOnly = false;
                LunaNUD.ReadOnly = false;
                LunaNUD.Enabled = true;
            }
            Int16 lunaC = Convert.ToInt16(((DataRowView)amixSetupBindingSource.Current)["LunaCurenta"]);
            if(lunaC<12)
                LunaNUD.Maximum = lunaC + 1;
            InchideConexiune(this.amixSetupTableAdapter.Connection);
        }

        private void relationTextBox1_ButtonClick(object sender, EventArgs e)
        {
            FrmMainMdi frm = this.MdiParent as FrmMainMdi;
            if (frm == null)
                frm = StudioForm.staticMdiParent as FrmMainMdi;
            if (frm == null)
                return;
            Assembly asm = frm.GetModule("erpforms");
            if (asm == null)
                return;

            String numeClasa = "ERPForms.SeriiNumericeRezumatFrm";
            Type mModule = asm.GetType(numeClasa);

            if (mModule != null)
            {
                FrmRezumat newFrm = asm.CreateInstance(numeClasa, true, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null,
                     new object[] { ModuleInfo }, Thread.CurrentThread.CurrentCulture, new object[] { }) as FrmRezumat;
                newFrm.SelectionAddOn = true;
                //newFrm.MdiParent = frm;
                if (newFrm.ShowDialog() == DialogResult.OK)
                {
                    //daca s-a selectat un rand se pune in text box valoare
                    ((RelationTextBox.RelationTextBox)sender).Text = newFrm.selectedRow.Cells["IdSerieColumn"].Value.ToString();
                }

            }
        }
    }
}
